package org.hk.kg.web.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hk.kg.utils.Constants;
import org.hk.kg.web.BaseAction;
import org.hk.kg.web.HtmlRender;

public class InputFieldRender implements HtmlRender {
	private static Pattern CSS_PATTERN = Pattern.compile("type[=]{1}\"button\" action=\"([^\"]*)\"");
	private static final Pattern BUTTON_PATTERN = Pattern.compile("(value|action|target)[=]{1}[\"]([^\"]*)[\"]");
	private static final String BUTTON_HREF_LINE = "<a href=\"##action#\" target=\"#target#\">#text#</a>";
	
	public boolean validate(String line) {
		return line.matches("^.*\\<input\\s*type\\=\"button\".*$");
	}

	public String execute(String line, BaseAction context) {
		Matcher matcher = BUTTON_PATTERN.matcher(line);
		String button_href = BUTTON_HREF_LINE;
		while(matcher.find())
		{
			String attrName = matcher.group(1);
			String attrValue = matcher.group(2);
			
			if(attrName.equalsIgnoreCase(Constants.UI_CONFIG_BUTTON_VALUE_ATTR))
			{
				button_href = button_href.replace("#text#", attrValue);
			}
			else if(attrName.equalsIgnoreCase(Constants.UI_CONFIG_BUTTON_ACTION_ATTR))
			{
				button_href = button_href.replace("#action#", attrValue);
			}
		}
		return button_href;
	}
}
